package org.goodsl.mdd;

import groovy.lang.Closure;
import org.goodsl.mdd.validator.ClosureValidator;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Simon Xiao
 * Date: 12-11-14
 * Time: 下午5:24
 * To change this template use File | Settings | File Templates.
 */
public class ClosureValidatorDelegate<T> {
    public Object methodMissing(String methodName, Object args){
        String message = "";
        List argsList = (List)args;

        Closure closure = null;
        if (argsList == null || argsList.size() == 0)
            return null;
        if (argsList.get(0) instanceof String) {
            if (argsList.size() < 2)
                return null;
            if (!(argsList.get(1) instanceof Closure))
                return null;
            message = (String) argsList.get(0);
            closure = (Closure) argsList.get(1);
        } else if (argsList.get(0) instanceof Closure)
            closure = (Closure) argsList.get(0);

        if (closure == null)
            throw new ParseException("校验器 " + methodName + "定义不正确");

        ClosureValidator<T> validator = new ClosureValidator<T>(methodName, closure, message);
        return validator;

    }


}